from django.db import models
from django.utils.safestring import mark_safe

from api.utils import image_to_url
from app.models.category import Category


class Product(models.Model):
    category = models.ForeignKey(Category, default=None, verbose_name=u'类型')
    name = models.CharField(max_length=30, verbose_name=u'名称')
    description = models.TextField(verbose_name=u'描述')
    image = models.ImageField(verbose_name=u'图片', )
    is_on_shell = models.BooleanField(default=True, verbose_name=u'上架')
    price = models.CharField(max_length=30, null=True, blank=True, verbose_name=u'价格')
    unit = models.CharField(max_length=30, null=True, blank=True, verbose_name=u'单位')

    class Meta:
        default_related_name = 'specs'
        ordering=["name"]
        verbose_name = u'商品'
        verbose_name_plural = u'商品'

    def __str__(self):
        return self.name

    @property
    def product_id(self):
        return self.id

    @property
    def image_url(self):
        return image_to_url(self.image)

    @property
    def 图片(self):
        if self.image:
            return mark_safe(
                '<div style="height: 40px;width: 60px"><a href="%s"><img style="max-height: 40px;max-width: 60px" src="%s"/></a></div>' % (
                    self.image.url, self.image.url))
        else:
            return '(no image)'


class ProductSpec(models.Model):
    product = models.ForeignKey(Product, related_name='specs', verbose_name=u'商品')
    name = models.CharField(max_length=100, verbose_name=u'规格')
    description = models.TextField(default='', verbose_name=u'描述')
    unit = models.CharField(max_length=30, verbose_name=u'单位')
    price = models.IntegerField(default=0, verbose_name=u'价格（单位：分）')
    discount = models.IntegerField(null=True, blank=True, verbose_name=u'优惠价格（单位：分）')
    stock = models.IntegerField(default=0, verbose_name=u'库存')  # 库存
    image = models.ImageField(verbose_name=u'图片', null=True, blank=True)
    is_on_shell = models.BooleanField(default=True, verbose_name=u'上架')
    price_for_display = models.CharField(null=True, blank=True, max_length=30, verbose_name=u'价格（仅显示）')
    deposit = models.PositiveIntegerField(default=0, verbose_name=u'押金')
    deposit_desc = models.CharField(max_length=30, null=True, blank=True, verbose_name=u'押金说明')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'商品规格'
        verbose_name_plural = u'商品规格'

    @property
    def spec_id(self):
        return self.id

    @property
    def image_url(self):
        return image_to_url(self.image)

    @property
    def 图片(self):
        if self.image:
            return mark_safe(
                '<div style="height: 40px;width: 60px"><a href="%s"><img style="max-height: 40px;max-width: 60px" src="%s"/></a></div>' % (
                    self.image.url, self.image.url))
        else:
            return '(no image)'


class ProductDetailImage(models.Model):
    product = models.ForeignKey(Product, related_name='detail_images', verbose_name=u'商品')
    image = models.ImageField(verbose_name=u'图片')
    order = models.IntegerField(verbose_name=u'排序')
    description = models.TextField(verbose_name=u'描述')

    class Meta:
        verbose_name = u'图片'
        verbose_name_plural = u'商品图片详情'
        ordering = ['order']

    @property
    def image_url(self):
        return image_to_url(self.image)

    def __str__(self):
        return self.product.name

    @property
    def 图片(self):
        if self.image:
            return mark_safe(
                '<div style="height: 80px;width: 40px"><a href="%s"><img style="max-height: 80px;max-width: 40px" src="%s"/></a></div>' % (
                    self.image.url, self.image.url))
        else:
            return '(no image)'
